//
//  PPProgressiveLogoView.m
//  PPProgressiveLogoView
//
//  Created by Jinxiao on 9/7/16.
//  Copyright © 2016 MicroDream. All rights reserved.
//

#import "PPProgressiveLogoView.h"

@interface PPProgressiveLogoTileView : UIView

@end

@implementation PPProgressiveLogoTileView

@end


@interface PPProgressiveLogoView ()
@property (readwrite, nonatomic, strong) UIView *container;
@property (readwrite, nonatomic, strong) NSArray <PPProgressiveLogoTileView *> *tileViews;
@end

@implementation PPProgressiveLogoView

- (instancetype)init {
    self = [super init];

    self.clipsToBounds = NO;

    _tileSize = CGSizeMake(9, 9);
    _logoSize = CGSizeMake(6*_tileSize.width, 6*_tileSize.height);

    _container = [[UIView alloc] init];
    _container.backgroundColor = [UIColor clearColor];
    [self addSubview:_container];

    NSMutableArray *tileViews = [NSMutableArray array];

    for(NSInteger index = 0; index < 19; index++) {
        PPProgressiveLogoTileView *tileView = [[PPProgressiveLogoTileView alloc] init];
        tileView.alpha = 0;
        tileView.backgroundColor = [UIColor whiteColor];
        [_container addSubview:tileView];

        [tileViews addObject:tileView];
    }

    _tileViews = tileViews;

    return self;
}

- (void)layoutSubviews {
    [super layoutSubviews];

    _container.frame = CGRectMake((self.frame.size.width - _logoSize.width)/2, self.height - 79, _logoSize.width, _logoSize.height);

    for(NSInteger index = 0; index < _tileViews.count; index++) {
        PPProgressiveLogoTileView *tileView = _tileViews[index];
        tileView.frame = [self frameForTileView:tileView atIndex:index];
    }
}

- (void)setLogoColor:(UIColor *)logoColor {
    _logoColor = logoColor;

    [_tileViews enumerateObjectsUsingBlock:^(PPProgressiveLogoTileView *_Nonnull obj, NSUInteger idx, BOOL *_Nonnull stop) {
        obj.backgroundColor = logoColor;
    }];
}

- (void)setProgress:(double)progress {
    _progress = progress;

    NSInteger separator = (NSInteger)round(_tileViews.count*progress);

    [UIView animateWithDuration:0.3 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        for(NSInteger index = 0; index < _tileViews.count; index++) {
            PPProgressiveLogoTileView *tileView = _tileViews[index];

            tileView.alpha = index < separator ? 1 : 0;
        }
    } completion:nil];

    _container.alpha = progress;
}

- (void)setOffset:(CGFloat)offset {
    _offset = offset;

    UIEdgeInsets insets = UIEdgeInsetsMake(13, 0, 13, 0);

    if(offset <= _container.height + insets.top + insets.bottom) {
        _container.top = self.height - offset + insets.top;
    } else {
        _container.centerY = self.height - offset/2;
    }
}

- (CGRect)frameForTileView:(PPProgressiveLogoTileView *)tileView atIndex:(NSInteger)index {

    NSInteger row = 0;
    NSInteger column = 0;

    switch(index) {
        case 0: row = 3; column = 5; break;

        case 1: row = 2; column = 5; break;

        case 2: row = 1; column = 5; break;

        case 3: row = 0; column = 5; break;

        case 4: row = 0; column = 4; break;

        case 5: row = 0; column = 3; break;

        case 6: row = 0; column = 2; break;

        case 7: row = 0; column = 1; break;

        case 8: row = 0; column = 0; break;

        case 9: row = 1; column = 0; break;

        case 10: row = 2; column = 0; break;

        case 11: row = 3; column = 0; break;

        case 12: row = 4; column = 0; break;

        case 13: row = 5; column = 0; break;

        case 14: row = 5; column = 1; break;

        case 15: row = 5; column = 2; break;

        case 16: row = 5; column = 3; break;

        case 17: row = 5; column = 4; break;

        case 18: row = 4; column = 4; break;
    }

    return CGRectMake(_tileSize.width*column, _tileSize.height*row, _tileSize.width, _tileSize.height);
}

@end
